package com.yksj.healthtalk.ui.setting;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartControlClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.DialogUtils;
import com.yksj.healthtalk.utils.MD5Utils;
import com.yksj.healthtalk.utils.SharePreUtils;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.WheelUtils;

public class SettingPassWordUI extends BaseFragmentActivity implements OnClickListener {

	private EditText mNewPsd;//新密码
	private EditText mNewPsdAgain;//重复新密码
	private String pasagain;
	private SmartControlClient mSmartFoxClient;
	private EditText oldPsd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_password);
		initView();
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("修改密码");
		mSmartFoxClient=SmartControlClient.getControlClient();
		findViewById(R.id.next).setOnClickListener(this);
		mNewPsd = (EditText)findViewById(R.id.setting_new_psd);
		mNewPsdAgain = (EditText)findViewById(R.id.setting_new_psd_agin);
		oldPsd = (EditText)findViewById(R.id.setting_old_psd);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			SystemUtils.hideSoftBord(this,mNewPsd);
			finish();
			break;
			
		case R.id.next:
			String string = oldPsd.getText().toString().trim();
			if(string!=null&&mSmartFoxClient.getPassword().equals(MD5Utils.getMD5(string))){
				comfirm();
			}else{
				ToastUtil.showBasicShortToast(SettingPassWordUI.this, "当前密码输入错误");
			}
			break;
		}
		
	}
	
	
	private void comfirm() {
		if(!SystemUtils.isNetWorkValid(this)){
			ToastUtil.showShort(SettingPassWordUI.this, R.string.getway_error_note);
			return;
		}
		String pasnew = mNewPsd.getText().toString().trim();
		pasagain = mNewPsdAgain.getText().toString().trim();
		if ("".equals(pasnew)|| "".equals(pasagain)) {
			DialogUtils.getErrorDialog(SettingPassWordUI.this,
					getString(R.string.jiankangliao),
					getString(R.string.psw_toastNull)).show();
		}else if (pasnew.length() < 6 || pasnew.length()> 12||pasagain.length() < 6) {
			ToastUtil.showShort("您输入的密码应该是6-12位");
		}else if (!pasnew.equals(pasagain)) {
			DialogUtils.getErrorDialog(SettingPassWordUI.this,getString(R.string.jiankangliao),
					getString(R.string.psw_toastDiscord)).show();
		} else if(pasnew.replaceAll("[a-z]*[A-Z]*\\d*", "").length()!=0 || 
				  pasagain.replaceAll("[a-z]*[A-Z]*\\d*", "").length()!=0){
			DialogUtils.getErrorDialog(SettingPassWordUI.this,
					getString(R.string.jiankangliao),
					getString(R.string.psw_toastSpecialcharter)).show();
		}else {
			String password = MD5Utils.getMD5(pasnew);
			HttpRestClient.doHttpChangePassword(SmartFoxClient.getSmartFoxClient().getUserId(), password, new AsyncHttpResponseHandler(this){
				@Override
				public void onSuccess(String value) {
					if(value!=null && value.equals("1")){
						if (pasagain.trim().length() != 32&&!pasagain.trim().equals("")&&pasagain.length()!=0) {
						mSmartFoxClient.updateUserPassword(pasagain);
						SharePreUtils.updateUserLoginPasswd(mSmartFoxClient.getPassword());
						ToastUtil.showBasicShortToast(SettingPassWordUI.this, "修改成功..");
						finish();
						overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
						}
					}else{
						ToastUtil.showBasicShortToast(SettingPassWordUI.this, "修改失败,请稍后重试..");
					}
				}
			});
			WheelUtils.hideInput(this,mNewPsdAgain.getWindowToken());
		}
	}
	
}
